﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            var sql = "select * from Users";

            var sqlArt = "select * from Articles";

            var dataTable = DbHelper.GetDataTable(sqlArt);

            dataGridView1.DataSource = dataTable;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;

        }


        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;

            var sql = string.Format("select * from Articles where Title like'%{0}%'", title);

            var dataTable = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dataTable;
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>


        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editForm = new EditForm();
            var res = editForm.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var title = textBox1.Text;

                var sqlArt = string.Format("select * from Articles where Title like '%{0}%'", title);

                var dataTable = DbHelper.GetDataTable(sqlArt);

                dataGridView1.DataSource = dataTable;
            }
            else
            {
                MessageBox.Show("这个是No");
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>


        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var countries = (string)dataGridView1.SelectedRows[0].Cells["Countries"].Value;


                EditForm editForm = new EditForm(id, title, author, countries);
                var res=editForm.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var title1 = textBox1.Text;

                    var sqlArt = string.Format("select * from Articles where Title like '%{0}%'", title);

                    var dataTable = DbHelper.GetDataTable(sqlArt);

                    dataGridView1.DataSource = dataTable;
                }
                else
                {
                    MessageBox.Show("这个是No");
                }

            }
            else
            {
                MessageBox.Show("您未选择任何数据", "提示");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("是否删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);

                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);

                    if (rowCount > 0)
                    {
                        var title = textBox1.Text;

                        var sqlArt = string.Format("select * from Articles where Title like '%{0}%'", title);

                        var dataTable = DbHelper.GetDataTable(sqlArt);

                        dataGridView1.DataSource = dataTable;

                        MessageBox.Show("删除成功", "提示");
                    }
                }
            }
            else
            {
                MessageBox.Show("没有选择任何行", "提示");
            }

        }
    }
}
